"use strict";
/* Controllers */
app.controller("smartMeterItemController", ['$scope', '$location', '$rootScope', '$routeParams', 'smartMeterItemService', function ($scope, $location, $rootScope, $routeParams, smartMeterItemService) {

    var vm = $scope.vm = {};
    vm.p = {};vm.d = {};vm.query={};
    vm.result = {};
    vm.p.type = $routeParams.type;
    if(vm.p.type=='id') {
        vm.p.id = $routeParams.id;
    }else {
        vm.p.queryCode = $routeParams.id;
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.query = function() {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 请求参数
        var params = vm.d;
        // 结果处理
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.query.data = data;
        };
        smartMeterItemService.query(params, procFn, errorFn);
    }

    $scope.getQueryCmd = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 请求参数
        var params = {
            id: vm.p.id,
            code: vm.p.queryCode
        };

        // 结果处理
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            vm.p.id = data.id;
            vm.d = data;
        };

        smartMeterItemService.getQueryCmd(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';

        $scope.getQueryCmd();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('smartMeterItemService', ['ajaxService', function (ajaxService) {

    this.getQueryCmd = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/smartdq/getQueryCmd.do", successFunction, errorFunction);
    };

    this.query = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/smartdq/query.do", successFunction, errorFunction);
    };
}]);


function columnNameFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return value + '('+row.columnNameCn+')';
}


